package com.common.transparent.module;

import android.content.Context;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.WindowManager;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created : wen
 * DateTime : 2017/9/20 23:46
 * Description :
 */
public class CameraUtil {
    /**
     * 获取后摄像头的id
     *
     * @return 摄像头id
     */
    public static Camera.CameraInfo getBackCameraInfo() {
        int num = Camera.getNumberOfCameras();
        for (int i = 0; i < num; i++) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(i, info);
            // 后摄像头
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return info;
            }
        }
        return null;
    }

    /**
     * 设置相机的方向
     *
     * @param context
     * @param info
     * @param camera
     */
    public static int setCameraDisplayOrientation(Context context, Camera.CameraInfo info, Camera camera) {

        int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();

        int degrees = 0;
        // rotation * 90
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        return result;
    }


    /**
     * 获取最适合的预览大小
     *
     * @param context
     * @param camera
     * @return
     */
    public static Camera.Size getPreviewSize(Context context, Camera camera) {

        Camera.Parameters parameters = camera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getMetrics(metrics);

        final double aspectRatio = metrics.heightPixels * 1.0 / metrics.widthPixels;

        Collections.sort(previewSizes, new Comparator<Camera.Size>() {
            @Override
            public int compare(Camera.Size s1, Camera.Size s2) {

                double aspectRatio1 = s1.height * 1.0 / s1.width;
                double aspectRatio1a = s1.width * 1.0 / s1.height;

                double aspectRatio2 = s2.height * 1.0 / s2.width;
                double aspectRatio2a = s2.width * 1.0 / s2.height;

                double diff1 = Math.min(Math.abs(aspectRatio - aspectRatio1), Math.abs(aspectRatio - aspectRatio1a));
                double diff2 = Math.min(Math.abs(aspectRatio - aspectRatio2), Math.abs(aspectRatio - aspectRatio2a));

                if (diff1 != diff2) {
                    if (diff1 < diff2)
                        return -1;
                    else
                        return 1;
                }

                int pixel1 = Math.abs(s1.height * s1.width);
                int pixel2 = Math.abs(s2.height * s2.width);
                return pixel2 - pixel1;
            }
        });
        return previewSizes.get(0);
    }
}
